﻿
namespace Caffeine.Core
{
    public static class ModelCollectionExtension
    {
        /// <summary>
        /// Chain an additional validation method to be called after the validation method already assigned is called. If no value is
        /// already assigned to the ValidationMethod property, the <paramref name="validationMethod"/> parameter value will be assigned 
        /// to it.
        /// </summary>
        /// <typeparam name="T">Type of value contained by the property</typeparam>
        /// <param name="property">Property to validate</param>
        /// <param name="validationMethod">Method used to validate property</param>
        public static void ChainValidationMethod<T>(this ModelCollection<T> property, CollectionValidationMethod<T> validationMethod)
            where T : ModelBase, new()
        {
            if (validationMethod != null)
            {
                if (property.ValidationMethod == null)
                {
                    property.ValidationMethod = validationMethod;
                }
                else
                {
                    property.ValidationMethod = (new CollectionValidationMethodWrapper<T>(property.ValidationMethod, validationMethod)).ValidationMethod;
                }
            }
        }
    }
}
